<?php
/*
 * 不良品处理方法
 */
class unqualitydb
{
	/**
	 * 通过计划员,供应商,处理日期,质检日期,SKU,VMI预约单号到unqualified_deal_invoices中找到对应的不良产品信息
	 * Enter description here ...
	 * @param unknown_type $condition
	 */
	public static function get_info($condition,$status,$orms)
	{
		$result=array();
		//计划员
		if(isset($condition['purchase_man_name']) and trim($condition['purchase_man_name']))
		{
			$purchase_man_name=trim($condition['purchase_man_name']);
			$nick_id=Erpuser::lookup_by_nick($purchase_man_name);
			$orms->where('purchase_man', '=', $nick_id);
		}
		//条件sku
		if(isset($condition['sku']) and trim($condition['sku']))
		{				
			$sku=trim($condition['sku']);
			$orms->where('sku', '=', "$sku");
		}
        //处理人
        if(isset($condition['operate_man']) and trim($condition['operate_man']))
        {
            $operate_man_name=trim($condition['operate_man']);
            $op_id=Erpuser::lookup_by_nick($operate_man_name);
            $orms->where('operate_man', '=', $op_id);
        }
		//条件供应商
		if(isset($condition['supplier']) and trim($condition['supplier']))
		{
			$supplier=trim($condition['supplier']);
			$supplier=basic::getidByName($supplier);	
			$orms->where('supplier', '=', $supplier);
		}
		//条件VMI预约单号
		if(isset($condition['purchaseorder_id']) and trim($condition['purchaseorder_id']))
		{				
			$purchaseorder_id=trim($condition['purchaseorder_id']);
			$orms->where('purchaseorder_id', '=', "$purchaseorder_id");
		}
		//处理人
		/*if(isset($condition['operate_man']) and trim($condition['operate_man']))
		{				
			$operate_man=trim($condition['operate_man']);
			$nick_id=Erpuser::lookup_by_nick($operate_man);
			if($nick_id=='')$nick_id=1;
			$orms->where('operate_man', '=', $nick_id);
		}
		//质检员
		if(isset($condition['quality_man']) and trim($condition['quality_man']))
		{				
			$quality_man=trim($condition['quality_man']);
			$nick_id=Erpuser::lookup_by_nick($quality_man);
			$orms->where('quality_man', '=', $nick_id);
		}*/
		//条件质检日期
		if(isset($condition['date_from']) and trim($condition['date_from']))
		{
			$date_from=trim($condition['date_from']);
			$t=strtotime($date_from);
			$date_from=date("Y-m-d H:i:s",$t);
			$orms->where('check_time', '>', "$date_from");
		}
		if(isset($condition['date_to']) and trim($condition['date_to']))
		{
			
			$date_to_1=trim($condition['date_to']);
			
			$t=strtotime($date_to_1);
			$date_to=date("Y-m-d H:i:s",$t+24*3600);
			
			$orms->where('check_time', '<', "$date_to");
		}
		//条件处理时间
		if(isset($condition['date_from_c']) and trim($condition['date_from_c']))
		{
			$date_from_c=trim($condition['date_from_c']);
			$t=strtotime($date_from_c);
			$date_from_c=date("Y-m-d H:i:s",$t);
			$orms->where('deal_time', '>', $date_from_c);
		}
		if(isset($condition['date_to_c']) and trim($condition['date_to_c']))
		{
			$date_to_c=trim($condition['date_to_c']);
			$t=strtotime($date_to_c);
			$date_to_c=date("Y-m-d H:i:s",$t+24*3600);				
			$orms->where('deal_time', '<', $date_to_c);
		}
		if(isset($condition['warehouse_man']) and trim($condition['warehouse_man']))
		{				
			$warehouse_man=trim($condition['warehouse_man']);
			$nick_id=Erpuser::lookup_by_nick($warehouse_man);
			$orms->where('warehouse_man', '=', $nick_id);
		}

		//库内非库内
        if(isset($condition['no_purchaseorder_id']) and $condition['no_purchaseorder_id'] != 0)
        {
            $orms->where('purchaseorder_id', $condition['no_purchaseorder_id'] == 10 ? '=' : '>', 0);
        }
		//查询全部不合格的产品
		if($status!=1)
		{
			$orms->where('status','=',$status);
		}
		//查询全部不合格处理方式的产品
		if(isset($condition['fail_deal_way']) &&$condition['fail_deal_way']&& $condition['fail_deal_way']!=1000)
		{
			$ctrl = is_array($condition['fail_deal_way'])?'in':'=';
			$orms->where('fail_deal_way',$ctrl,$condition['fail_deal_way']);
		}
		if($_GET['tableName']=='unqualified_deal_invoices'){
			$orms = $orms->order_by('invoice_date','desc');
		}else{
			$orms = $orms->order_by('id','DESC');
		}
		$result=$orms->execute('newerp')->as_array();
		return $result;	
	}
}
